package parser;

import java.util.Calendar;
import java.util.GregorianCalendar;

import utils.Statics;
/**
 * class contenant une methode static pour parser une date en format string vers un objet gregoriancalendar
 * Collection Time: 16/09/2009 17:16:21
 * @author balliau
 *
 */
public class date_parser {

	public static Long parsedate(String mydate, boolean debug){
//		if(mydate.contains("/")){
//			String day, month, year, hour, minute, seconde;
//			int index = mydate.indexOf("/");
//			day = mydate.substring(0, index).trim();
//			mydate = mydate.substring(index+1);
//			index = mydate.indexOf("/");
//			month = mydate.substring(0, index).trim();
//			mydate = mydate.substring(index+1);
//			index = mydate.indexOf(" ");
//			year = mydate.substring(0, index).trim();
//			mydate = mydate.substring(index+1);
//			index = mydate.indexOf(":");
//			hour = mydate.substring(0, index).trim();
//			mydate = mydate.substring(index+1);
//			index = mydate.indexOf(":");
//			minute = mydate.substring(0, index).trim();
//			seconde = mydate.substring(index+1, mydate.length()).trim();
//			if(Statics.debug){
//				System.out.println(day);
//				System.out.println(month);
//				System.out.println(year);
//				System.out.println(hour);
//				System.out.println(minute);
//				System.out.println(seconde);
//			}
//			GregorianCalendar date = new GregorianCalendar();
//			date.set(Integer.parseInt(year.trim()), Integer.parseInt(month.trim()), Integer.parseInt(day.trim()), Integer.parseInt(hour.trim()), Integer.parseInt(minute.trim()), Integer.parseInt(seconde.trim()));
//			return date;
//		}else{
			String hour, minute, seconde, millis;
			int index = mydate.indexOf(":");
			hour = mydate.substring(0, index).trim();
			mydate = mydate.substring(index+1);
			index = mydate.indexOf(":");
			minute = mydate.substring(0, index).trim();
			seconde = mydate.substring(index+1, mydate.indexOf(".")).trim();
			millis = mydate.substring(mydate.indexOf(".")+1, mydate.length() ).trim();
			//GregorianCalendar date = new GregorianCalendar();
			Long time = Long.parseLong(millis)+(Long.parseLong(seconde)*1000)+(Long.parseLong(minute)*60000)+(Long.parseLong(hour)*3600000);
			return time;
//			date.setTimeInMillis(time);
//			return date;
//		}
	}
	/**
	 * retourne une date preformaté 
	 *si format = 0 -> jour-mois-année
	 *si format = 1 -> jour/mois/année
	 *si format = 2 -> jour-mois-année hh:min
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getdateformated(GregorianCalendar date, int format){
		String dateformated = null;
		if (format == 0){
			dateformated = ""+date.get(Calendar.DAY_OF_MONTH)+"-"+date.get(Calendar.MONTH)+"-"+date.get(Calendar.YEAR);
		}
		if (format == 1){
			dateformated = ""+date.get(Calendar.DAY_OF_MONTH)+"/"+date.get(Calendar.MONTH)+"/"+date.get(Calendar.YEAR);
		}
		if (format == 2){
			dateformated = ""+date.get(Calendar.DAY_OF_MONTH)+"-"+date.get(Calendar.MONTH)+"-"+date.get(Calendar.YEAR)+" "+date.get(Calendar.HOUR_OF_DAY)+":"+date.get(Calendar.MINUTE);
		}
		return dateformated;
	}
	public static String get_time_formated(Long timeinmillis){
		Long millis = timeinmillis%1000;
		timeinmillis = timeinmillis/1000;
		Long sec = timeinmillis%60;
		timeinmillis = timeinmillis/60;
		Long min = timeinmillis%60;
		Long hour = timeinmillis/60;
		return ""+hour+":"+min+":"+sec+"."+millis;
	}
}
